package com.greet.task;

import com.greet.websocket.WebSocketServer;
import com.greet.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private WebSocketServer webSocketServer;


    /*处理超时订单*/
    @Scheduled(cron = "0 1 * * * ? ")
    public void processTimeoutOrder(){
        //将超过15分钟的超时订单设置为已取消
        log.info("正在检查超时订单");
        LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(15L);
        String cancelReason = new String("订单超时， 自动取消");
        ordersMapper.updateCancelByTimeoutOrder(localDateTime, cancelReason);
    }

    /*处理一直在派送中的订单*/
    @Scheduled(cron = "0 0 1 * * ? ")
    public void processDeliveryOrder(){
        log.info("正在检正在派送超时订单");
        LocalDateTime localDateTime = LocalDateTime.now().minusHours(12L);
        ordersMapper.updateOnTaskByDeliveryOrder(localDateTime);
    }

}
